/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PaqueteForm.java
 *
 * Created on 29/03/2010, 11:53:13 AM
 */

package agencia.form;

import agencia.Agencia;
import agencia.TourPaquete;
import agencia.controller.AgenciaController;
import agencia.controller.LugarTuristicoController;
import agencia.controller.PaqueteController;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author elvis
 */
public class PaqueteListaForm extends javax.swing.JInternalFrame {

    private JDesktopPane jdesktopPane;
    private PaqueteController paqueteController;
    private AgenciaController agenciaController;
    private LugarTuristicoController lugarTuristicoController;

    public void setPaqueteController(PaqueteController paqueteController) {
        this.paqueteController = paqueteController;
    }
    public JDesktopPane getJdesktopPane() {
        return jdesktopPane;
    }
    public void setJdesktopPane(JDesktopPane jdesktopPane) {
        this.jdesktopPane = jdesktopPane;
    }
    public void setAgenciaController(AgenciaController agenciaController) {
        this.agenciaController = agenciaController;
    }
    /** Creates new form PaqueteForm */
    public PaqueteListaForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblPaquetes = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        cbxAgencia = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Paquetes Turisticos");

        tblPaquetes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Agencia", "Codigo", "Nombre", "Ninio", "Adulto", "Adulto Mayor"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblPaquetes);

        jButton3.setText("-");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("E");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        cbxAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxAgenciaActionPerformed(evt);
            }
        });

        jLabel1.setText("Agencia");

        jButton4.setText("Mostrar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cbxAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(91, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jButton4))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
         if (tblPaquetes.getSelectedRow()!=-1){
            int cod = (Integer)tblPaquetes.getValueAt( tblPaquetes.getSelectedRow(), 1);
             PaqueteForm form = new PaqueteForm();
            form.setAgenciaController(agenciaController);
            form.setPaqueteController(paqueteController);
            form.setLugarTuristicoController(lugarTuristicoController);
            form.setPaqueteListaForm(this);
            form.setPaquete(paqueteController.obtenerPaquete(cod));
            form.setAccion(PaqueteForm.ACCION_MODIFICAR);
            form.iniciaForm();
            jdesktopPane.add(form);
            form.show();
         }else{
          JOptionPane.showMessageDialog(rootPane, "Debe seleccionar el paquete a modificar");
         }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
         if (tblPaquetes.getSelectedRow()!=-1){
            int cod = (Integer)tblPaquetes.getValueAt( tblPaquetes.getSelectedRow(), 1);
//           paqueteController.e
         }else{
          JOptionPane.showMessageDialog(rootPane, "Debe seleccionar el paquete a eliminar");
         }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void cbxAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxAgenciaActionPerformed
        // cambio la lista de paquetes segun la agencia seleccionada
        

    }//GEN-LAST:event_cbxAgenciaActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        if(cbxAgencia.getSelectedItem().toString().equals("Todos")){
            pintagrilla(paqueteController.getListaToursPaquetes());
        }else{
        Agencia agencia =  agenciaController.obtenerAgencia(cbxAgencia.getSelectedItem().toString());
         pintagrilla(paqueteController.getListaToursPaquetes(agencia.getCodigo()));
        }
         
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        PaqueteForm form = new PaqueteForm();
        form.setAgenciaController(agenciaController);
        form.setPaqueteController(paqueteController);
        form.setLugarTuristicoController(lugarTuristicoController);
        form.setPaqueteListaForm(this);
        form.setAccion(PaqueteForm.ACCION_NUEVO);
        form.iniciaForm();
        jdesktopPane.add(form);
        form.show();
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxAgencia;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblPaquetes;
    // End of variables declaration//GEN-END:variables

      public void pintagrilla(List<TourPaquete> paquetes){
        DefaultTableModel tabla = (DefaultTableModel) tblPaquetes.getModel();
        tabla.setRowCount(0);
        for (TourPaquete examen : paquetes) {
            Object[] fila = new Object[6];
            fila[0] = examen.getAgencia().getNombre();
            fila[1] = examen.getCodigo();
            fila[2] = examen.getNombre();
            fila[3] = examen.getTarifaNinio();
            fila[4] = examen.getTarifaAdulto();
            fila[5] = examen.getTarifaadultoMayor();
            tabla.addRow(fila);
        }
    }

    public void iniciarForm() {
        cbxAgencia.removeAllItems();
        cbxAgencia.addItem("Todos");
        for(Agencia agencia : agenciaController.getListaAgencias()){
            cbxAgencia.addItem(agencia.getNombre());
        }

        pintagrilla(paqueteController.getListaToursPaquetes());
    }

    /**
     * @param lugarTuristicoController the lugarTuristicoController to set
     */
    public void setLugarTuristicoController(LugarTuristicoController lugarTuristicoController) {
        this.lugarTuristicoController = lugarTuristicoController;
    }


    

  
}
